﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CMS.IRepository;


namespace CMS.Repository
{
    public class StudentRepository : IStudentRepository
    {
        /// <summary>
        /// Selects all students.
        /// </summary>
        /// <returns>List of Student</returns>
        /// <remarks>RebeccaNiu</remarks>
        public List<CMS.Model.Student> SelectAllStudents(int pageIndex,int pageSize,out int rowCount)
        {
            int skipCount = (pageIndex - 1) * pageSize;
            using (CMSEntities edm = new CMSEntities())
            {
                List<Student> students = (from s in edm.Students
                                           orderby s.StudentId
                                          select s).Skip(skipCount).Take(pageSize).ToList<Student>();
                foreach (Student obj in students)
                {
                    obj.ClassReference.Load();
                }
                List<CMS.Model.Student> studentCourseDetail = Translator.StudentTranslator.ToStudentModelList(students);

                rowCount = (from s in edm.Students
                            select s).ToList().Count;
                return studentCourseDetail;
            }
        }

        /// <summary>
        /// Selects the student by id.
        /// </summary>
        /// <param name="studentId">The student id.</param>
        /// <returns>Model of Student</returns>
        /// <remarks>RebeccaNiu</remarks>
        public CMS.Model.Student SelectStudentById(int studentId)
        {
            Student stu;
            using (CMSEntities edm = new CMSEntities())
            {
                stu = (from s in edm.Students
                       where s.StudentId == studentId
                       select s).SingleOrDefault<Student>();
            }
            CMS.Model.Student result = Translator.StudentTranslator.ToStudentModel(stu);
            return result;
        }
        /// <summary>
        /// Inserts the student.
        /// </summary>
        /// <param name="student">The student.</param>
        /// <remarks>RebeccaNiu</remarks>
        public void InsertStudent(CMS.Model.Student student)
        {
            using (CMSEntities edm = new CMSEntities())
            {

                Student stu = Translator.StudentTranslator.ToStudentEF(student);
                edm.Students.AddObject(stu);
                edm.SaveChanges();
            }
        }
        /// <summary>
        /// Updates the student.
        /// </summary>
        /// <param name="student">The student.</param>
        /// <remarks>RebeccaNiu</remarks>
        public void UpdateStudent(CMS.Model.Student student)
        {
            using (CMSEntities edm = new CMSEntities())
            {
                Student stu = edm.Students.SingleOrDefault(s => s.StudentId == student.StudentId);
                Translator.StudentTranslator.ToStudentEF(student, stu);
                if (stu != null)
                {
                    edm.SaveChanges();
                }
            }
        }
        /// <summary>
        /// Deletes the student by id.
        /// </summary>
        /// <param name="studentId">The student id.</param>
        /// <remarks>RebeccaNiu</remarks>
        public void DeleteStudentById(int studentId)
        {
            using (CMSEntities edm = new CMSEntities())
            {
               var students = (from s in edm.Students
                               where s.StudentId == studentId
                               select s).ToList();
               for (int i = 0; i < students.Count; i++)
               {
                   int student_Id = students[i].StudentId;
                   var courseDetails = (from cd in edm.CourseDetails
                                        where cd.StudentId == student_Id
                                        select cd).ToList();
                   for (int j = 0; j < courseDetails.Count; j++)
                   {
                       int course_Id = courseDetails[j].CourseId;
                       edm.CourseDetails.DeleteObject(courseDetails[j]);
                   }
                   edm.Students.DeleteObject(students[i]);
               }
                if (students != null)
                {
                    edm.SaveChanges();
                }
            }
        }
        /// <summary>
        /// Selects the students not in course.
        /// </summary>
        /// <param name="courseId">The course id.</param>
        /// <returns></returns>
        public List<Model.Student> SelectStudentsNotInCourse(int courseId)
        {

            using (var edm = new CMSEntities())
            {

                var students = (from s in edm.Students
                                select s).Except(from c in edm.CourseDetails
                                                 join si in edm.Students
                                                 on c.StudentId equals si.StudentId
                                                 where c.CourseId == courseId
                                                 select si).ToList<Student>();
                return Translator.StudentTranslator.ToStudentModelList(students);
            }
        }
        /// <summary>
        /// Selects the students in course.
        /// </summary>
        /// <param name="courseId">The course id.</param>
        /// <returns></returns>
        public List<Model.Student> SelectStudentsInCourse(int courseId)
        {
            using (var edm = new CMSEntities())
            {
                var students = (from cd in edm.CourseDetails
                                join s in edm.Students
                                on cd.StudentId equals s.StudentId
                                where cd.CourseId == courseId
                                select s).ToList<Student>();

                return Translator.StudentTranslator.ToStudentModelList(students);
            }
        }

    }
}
